package com.cps116.resource;

import com.cps116.friendstracker.ProfileActivity;
import com.cps116.friendstracker.R;
import com.cps116.friendstracker.R.id;
import com.cps116.friendstracker.R.layout;
import com.cps116.friendstracker.R.string;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FriendsTrackerLoginActivity extends Activity{

	private Intent connectionEstablishedIntent;
	private Button loginButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginactivity_layout);

		//create the the intent for transferring to ProfileActivity 
		//once facebook login has been established...

		connectionEstablishedIntent = new Intent(getApplicationContext(), ProfileActivity.class);
		loginButton = (Button) findViewById(R.id.login_in_button);
		loginButton.setOnClickListener(clickListener);

	}


	private OnClickListener clickListener = new OnClickListener() {

		public void onClick(View view) {
			//we check if our connection is properly set up...

			//TOOD: need to test if this works or not!
			try{
				ConnectivityManager cMgr = (ConnectivityManager) view.getContext().getSystemService(Context.CONNECTIVITY_SERVICE); 		
				NetworkInfo netInfo = cMgr.getActiveNetworkInfo();
				String status = netInfo.getState().toString();

				if (status.equals(R.string.login_activity_connected_msg)) {
					//check if the facebook user/pass are valid
					//**don't need to use AsyncTask here because 
					boolean login_successful = attemptFbLogin();
					if (login_successful)
						startActivity(connectionEstablishedIntent);
					else
						//display the msg of failed login
						Toast.makeText(view.getContext(), R.string.login_activity_failed_fbconnect_msg, Toast.LENGTH_SHORT).show();
						
				} else {
					Toast.makeText(getApplicationContext(), R.string.login_activity_no_connection_msg,
							Toast.LENGTH_SHORT).show(); 
				}
			}
			catch (Exception e) {
				Toast.makeText(getApplicationContext(), R.string.login_activity_connect_denied_msg,
						Toast.LENGTH_SHORT).show(); 
			}
		}

		
		/**
		 * Here we are trying to establish a connection to facebook
		 * with user input and output
		 * 
		 */
		private boolean attemptFbLogin() {
			
			String username = ((EditText) findViewById(R.id.login_activity_username_textfield)).getText().toString();
			String password = ((EditText) findViewById(R.id.login_activity_password_textfield)).getText().toString();
			
			
			
			
			
			
			return false;
		}
	};
	
	
}